package main

import (
	"context"
	"google.golang.org/grpc"
	pb "grpc-microservice/proto"
	"log"
	"time"
)

func main() {
	// 连接 gRPC 服务器
	conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithInsecure(), grpc.WithBlock())

	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}

	defer conn.Close()

	client := pb.NewUserServiceClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	// 调用 GetUser
	getUserResp, err := client.GetUser(ctx, &pb.GetUserRequest{Id: 1})
	if err != nil {
		log.Fatalf("getUser error : %v", err)
	}

	log.Printf("getUserResp : %v", getUserResp)

	// 调用创建用户
	createUserResp, err := client.CreateUser(ctx, &pb.CreateUserRequest{Username: "ADMIN", Email: "admin@gmail.com"})
	if err != nil {
		log.Fatalf("createUser error : %v", err)
	}
	log.Printf("createUserResp : %v", createUserResp)

}
